local hx__chuandi = fk.CreateSkill {
  name = "hx__chuandi&",
}

local HX = require "packages/huixiangkill/hxfs"

hx__chuandi:addEffect("active", {
  anim_type = "special",
  prompt = "#hx__chuandi",
  card_num = 0,
  target_num = 1,
  can_use = function(self, player)
    return #player:getTableMark("@&hx__moyin_emoji") > 1
  end,
  interaction = UI.ComboBox { choices = {"hx__chuandi-maxhp", "hx__chuandi-damage"} },
  card_filter = Util.FalseFunc,
  target_filter = function (self, player, to_select, selected)
    return #selected == 0 and to_select ~= player
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local to = effect.tos[1]
    local mark = player:getTableMark("@&hx__moyin_emoji")
    local toremove = HX.ChuanDi(room, player, mark, hx__chuandi.name)
    room:sendLog{
      type = "#hx__chuandi-log",
      from = player.id,
      to = { to.id },
      arg = Fk:translate(toremove[1]),
      arg2 = Fk:translate(toremove[2]),
    }
    HX.AfterChuanDi(room, player, mark, to, toremove, hx__chuandi.name)
    if self.interaction.data == "hx__chuandi-maxhp" then
      room:changeMaxHp(player, 1)
      room:recover{
        who = player,
        num = 1,
        recoverBy = player,
        skillName = hx__chuandi.name,
      }
    else
      local tar = room:askToChoosePlayers(player,{
        min_num = 1,
        max_num = 1,
        skill_name = hx__chuandi.name,
        targets = room.alive_players,
        cancelable = false,
        prompt = "#hx__chuandi-damage",
      })[1]
      room:damage{
        from = player,
        to = tar,
        damage = 1,
        skillName = hx__chuandi.name,
      }
    end
  end,
})

hx__chuandi:addEffect(fk.EnterDying, {
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(hx__chuandi.name) and #player:getTableMark("@&hx__moyin_emoji") > 1
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local tar = room:askToChoosePlayers(player,{
      min_num = 1,
      max_num = 1,
      skill_name = hx__chuandi.name,
      targets = room:getOtherPlayers(player),
      cancelable = true,
      prompt = "#hx__chuandi-ask1",
    })
    if tar then
      local mark = player:getTableMark("@&hx__moyin_emoji")
      local toremove = HX.ChuanDi(room, player, mark, hx__chuandi.name)
      event:setCostData(self, {
        toremove = toremove,
        mark = mark,
        to = tar[1],
      })
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local toremove = event:getCostData(self).toremove
    local mark = event:getCostData(self).mark
    local to = event:getCostData(self).to
    room:sendLog{
      type = "#hx__chuandi-log",
      from = player.id,
      to = { to.id },
      arg = Fk:translate(toremove[1]),
      arg2 = Fk:translate(toremove[2]),
    }
    HX.AfterChuanDi(room, player, mark, to, toremove, hx__chuandi.name)

    room:changeMaxHp(player, 1)
    room:recover{
      who = player,
      num = 1,
      recoverBy = player,
      skillName = hx__chuandi.name,
    }
  end,
})

hx__chuandi:addEffect(fk.BeforeTurnOver, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(hx__chuandi.name) and #player:getTableMark("@&hx__moyin_emoji") > 1
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local tar = room:askToChoosePlayers(player,{
      min_num = 1,
      max_num = 1,
      skill_name = hx__chuandi.name,
      targets = room:getOtherPlayers(player),
      cancelable = true,
      prompt = "#hx__chuandi-ask2",
    })
    if tar then
      local mark = player:getTableMark("@&hx__moyin_emoji")
      local toremove = HX.ChuanDi(room, player, mark, hx__chuandi.name)
      event:setCostData(self, {
        toremove = toremove,
        mark = mark,
        to = tar[1],
      })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local toremove = event:getCostData(self).toremove
    local mark = event:getCostData(self).mark
    local to = event:getCostData(self).to
    room:sendLog{
      type = "#hx__chuandi-log",
      from = player.id,
      to = { to.id },
      arg = Fk:translate(toremove[1]),
      arg2 = Fk:translate(toremove[2]),
    }
    HX.AfterChuanDi(room, player, mark, to, toremove, hx__chuandi.name)

    data.prevented = true
    to:turnOver()
  end,
})

hx__chuandi:addEffect(fk.BeforeChainStateChange, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(hx__chuandi.name) and #player:getTableMark("@&hx__moyin_emoji") > 1
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local tar = room:askToChoosePlayers(player,{
      min_num = 1,
      max_num = 1,
      skill_name = hx__chuandi.name,
      targets = room:getOtherPlayers(player),
      cancelable = true,
      prompt = "#hx__chuandi-ask3",
    })
    if tar then
      local mark = player:getTableMark("@&hx__moyin_emoji")
      local toremove = HX.ChuanDi(room, player, mark, hx__chuandi.name)
      event:setCostData(self, {
        toremove = toremove,
        mark = mark,
        to = tar[1],
      })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local toremove = event:getCostData(self).toremove
    local mark = event:getCostData(self).mark
    local to = event:getCostData(self).to
    room:sendLog{
      type = "#hx__chuandi-log",
      from = player.id,
      to = { to.id },
      arg = Fk:translate(toremove[1]),
      arg2 = Fk:translate(toremove[2]),
    }
    HX.AfterChuanDi(room, player, mark, to, toremove, hx__chuandi.name)

    data.prevented = true
    if player.chained and to.chained then
      to:setChainState(false)
    elseif not player.chained and not to.chained then
      to:setChainState(true)
    end
  end,
})


return hx__chuandi
